package com.example.demo1.generator.codedesign;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

public class ResponseBuilder {

	public final static int CODE_OK = 200;

	/** 服务器错误 */
	public final static int CODE_ERROR = 500;

	/**
	 * 成功调用返回
	 * 
	 * @return
	 */
	public static String ok() {
		Map<String, Object> result = new HashMap<>();
		result.put("code", CODE_OK);
		result.put("liangxin-version", CODE_OK);
		return formatJson(result);
	}

	/**
	 * 成功调用返回
	 * 
	 * @param data
	 *            返回对象
	 * @return
	 */
	public static String ok(Object data) {
		Map<String, Object> result = new HashMap<>();
		result.put("code", CODE_OK);
		result.put("data", data);
		return formatJson(result);
	}

	public static String error() {
		Map<String, Object> result = new HashMap<>();
		result.put("code", CODE_ERROR);
		return formatJson(result);
	}

	public static String fail(int code) {
		Map<String, Object> result = new HashMap<>();
		result.put("code", code);
		return formatJson(result);
	}

	public static String formatJson(Object data) {
		return JSON.toJSONString(data);
	}
}
